/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.form.core;

import com.blandware.atleap.webapp.form.BaseForm;
import com.blandware.atleap.webapp.util.core.WebappConstants;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;
import org.apache.struts.upload.MultipartRequestHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>Form bean for upload content resource
 * </p>
 * <p><a href="UploadContentResourceForm.java.html"><i>View Source</i></a></p>
 * <p/>
 *
 * @author Andrey Grebnev <a href="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
 * @version $Revision: 1.11 $ $Date: 2007/04/06 10:56:09 $
 * @struts.form name="uploadContentResourceForm"
 */
public class UploadContentResourceForm extends BaseForm {

	/**
	 * The file that the user has uploaded
	 */
	protected transient FormFile file;

    /**
     * Initial URI component
     */
    protected String folder;

    /**
     * Required type
     */
    protected String type;

    /**
	 * Retrieves a representation of the file the user has uploaded
	 *
	 * @return FormFile the uploaded file
	 */
	public FormFile getFile() {
		return file;
	}

	/**
	 * Sets a representation of the file the user has uploaded
	 *
	 * @param file the file to upload
	 */
	public void setFile(FormFile file) {
		this.file = file;
	}

    /**
     * Returns initial URI component.
     *
     * @return folder
     */
    public String getFolder() {
        return folder;
    }

    /**
     * Sets initial URI component.
     *
     * @param folder folder to set
     */
    public void setFolder(String folder) {
        this.folder = folder;
    }

    /**
     * Returns required type.
     *
     * @return type
     */
    public String getType() {
        return type;
    }

    /**
     * Sets required type.
     *
     * @param type type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
	 * Check to make sure the client hasn't exceeded the maximum allowed upload size inside of this
	 * validate method.
	 */
	// Commented out to avoid: Unhandled Exception thrown: class java.lang.NullPointerException
	public ActionErrors validate(ActionMapping mapping,
	                             HttpServletRequest request) {
		ActionErrors errors = null;
		// has the maximum length been exceeded?
		Boolean maxLengthExceeded =
		        (Boolean) request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
		if ( (maxLengthExceeded != null) && (maxLengthExceeded.booleanValue()) ) {
			errors = new ActionErrors();
			errors.add("contentResourceMaxLengthExceeded",
			        new ActionMessage("core.contentResource.errors.maxLengthExceeded"));
            request.getSession().setAttribute(WebappConstants.ERROR_KEY, errors);
		}
		return errors;
	}

}